package com.xinlong.controller;


import com.github.pagehelper.PageInfo;
import com.xinlong.entity.SchedulerJob;
import com.xinlong.service.SchedulerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("scheduler")
public class SchedulerController {

    @Autowired
    private SchedulerService schedulerServiceImpl;

    @RequestMapping("list")
    public ModelAndView manage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5")Integer size, @RequestParam(defaultValue = "0")Integer triggerStatus) throws Exception{
        ModelAndView modelAndView = new ModelAndView("scheduler-list");
        modelAndView.addObject("schedulerList",schedulerServiceImpl.show(page,size,triggerStatus));
        return modelAndView;
    }

    @RequestMapping("save")
    public ModelAndView save(SchedulerJob job)throws Exception{
        ModelAndView modelAndView = new ModelAndView("redirect:/scheduler/list.do");
        schedulerServiceImpl.add(job);
        return modelAndView;
    }


    @RequestMapping("pause")
    @ResponseBody
    public Integer pause(String name, String group) throws Exception{
        int result = schedulerServiceImpl.pause(name,group);
        return result;
    }

    @RequestMapping("resume")
    @ResponseBody
    public Integer resume(String name,String group) throws Exception{
        int result = schedulerServiceImpl.resume(name,group);
        return result;
    }

    @RequestMapping("trigger")
    @ResponseBody
    public Integer trigger(String name, String group) throws Exception{
        int result = schedulerServiceImpl.execute(name,group);
        return result;
    }

    @RequestMapping("delete")
    @ResponseBody
    public Integer delete(String name, String group) throws Exception{
        int result = schedulerServiceImpl.delete(name,group);
        return result;
    }


    @RequestMapping("/toAdd")
    public ModelAndView toAdd(){
        ModelAndView modelAndView = new ModelAndView("scheduler-add");
        return modelAndView;
    }

    @RequestMapping("/toEdit")
    public ModelAndView toEdit(String name,String group)throws Exception{
        ModelAndView modelAndView = new ModelAndView("scheduler-add");
        SchedulerJob job = schedulerServiceImpl.selByNameAndGroup(name,group);
        modelAndView.addObject("scheduler",job);
        return modelAndView;
    }








}
